(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['exports'], factory);
    } else if (typeof module === 'object' && module.exports) {
        module.exports = factory(exports);
    } else {
        root.RefDecoder = factory();
    }
}(typeof self !== 'undefined' ? self : this, function () {
    var RefDecoder = function(prevNode, prefixLen, suffixLen) {
        this.prevNode = prevNode;
        this.prefixLen = prefixLen;
        this.suffixLen = suffixLen;
        // console.log("this.prevNode:", this.prevNode, "this.prefixLen:", this.prefixLen, "this.suffixLen:", this.suffixLen);
    }
    RefDecoder.prototype = {}

    /**
     * Inflates the Kaitai stream.
     * @param data The data to inflate. If there is no data and a stream was provided in the constructor, the stream is used instead.
     * @returns The inflated data.
     */
    RefDecoder.prototype.decode = function(data) {
        let suffix = (new TextDecoder()).decode(data);
        // console.log("suffix: ", suffix);
        
        let prefix = "";
        if (this.prevNode && this.prefixLen > 0) {
            prefix = this.prevNode.refname.fullname.slice(0, this.prefixLen);
        }

        let fullname = prefix + suffix;
        // console.log("fullname: ", fullname);
        
        return {                
            "fullname": fullname,
            "prefix": prefix,
            "prefixLen": this.prefixLen,
            "suffix": suffix,
            "suffixLen": this.suffixLen
        }            
    }

    return RefDecoder;
}));
